package cn.vihub.controller;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import cn.vihub.param.ShopGoodsCategoryParam;
import cn.vihub.pojo.ShopGoodsCategory;
import cn.vihub.service.ShopGoodsCategoryService;
import cn.vihub.utils.Result;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 秋晚
 * @Date: 2025/7/28 19:34
 * @Description:
 **/
@RestController
@RequestMapping("shop-store/shopGoodsCategory")
public class ShopGoodsCategoryController {

    @Autowired
    ShopGoodsCategoryService shopGoodsCategoryService;
    /**
     * @author: 秋晚
     * @description:查询所有分类
     * @params: []
     * @return: cn.vihub.utils.Result<java.lang.Object>
     * @date: 2025/7/28 20:01
     */
    @GetMapping("tree")
    public Result<Object> getTree(){
        return Result.success(shopGoodsCategoryService.getTree());
    }


    /**
     * @author: 秋晚
     * @description:添加或编辑分类
     * @params: [cn.vihub.param.ShopGoodsCategoryParam]
     * @return: cn.vihub.utils.Result<java.lang.Boolean>
     * @date: 2025/7/28 20:09
     */
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody ShopGoodsCategoryParam shopGoodsCategoryParam){
        //创建对象
        ShopGoodsCategory shopGoodsCategory = new ShopGoodsCategory();
        //拷贝
        BeanUtils.copyProperties(shopGoodsCategoryParam,shopGoodsCategory);
        //返回
        return Result.success(shopGoodsCategoryService.saveOrUpdate(shopGoodsCategory));
    }


    /**
     * @author: 秋晚
     * @description:根据id删除分类
     * @params: [java.lang.Integer]
     * @return: cn.vihub.utils.Result<java.lang.Boolean>
     * @date: 2025/7/28 20:10
     */
    @DeleteMapping("delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Integer id){
        return Result.success(shopGoodsCategoryService.removeById(id));
    }
}
